package cn.web.web_module.rabbitmq.consumber;

import cn.itcast.mapper.accountMapper;
import cn.itcast.mapper.earningsMapper;
import cn.itcast.pojo.account;
import cn.itcast.pojo.earnings;
import cn.itcast.util.DoubleUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.Map;

import static cn.itcast.util.OrderNumber.getOrderIdByTime;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 10:02 2021/4/1
 * @Modified By:
 */
@Slf4j
@Component
@RabbitListener(queues = {"direct_EarningInfo_Queue"})
public class DirectEarningInfoConsumber {

    @Resource
    private earningsMapper earningsMapper;

    @Resource
    private accountMapper accountMapper;

    @RabbitHandler
    public void EarningInfoConsumber(Map<String,String> notifyUrlParameter){
        String openid = notifyUrlParameter.get("openid");
        account account = accountMapper.selectByAccountOpenid(openid);
        double total_fee = Double.parseDouble(notifyUrlParameter.get("total_fee"))/100;
        earnings earnings = new earnings();
        earnings.setAccountId(account.getAccountId());
        earnings.setName(account.getAccountName());
        earnings.setPhone(account.getAccountPhone());
        earnings.setOpenid(account.getOpenId());
        earnings.setSource("微信充值");
        earnings.setNumber(getOrderIdByTime());
        earnings.setNum(total_fee);
        earnings.setState("已到账");
        earnings.setEarningsExplain("微信充值");
        //用户充值之后的余额
        earnings.setCountnum(DoubleUtils.add(account.getAccountMoneyBalance(),total_fee));
        earningsMapper.insertSelective(earnings);
        log.info(account.getAccountName()+":充值"+account.getAccountMoneyBalance()+"元,earnings表消息记录成功!");
    }

}
